// hyb_yl/czhuanjiasubpages/pages/questends/index.js
var app = getApp()
var Utils = require('../../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    oneinfo: [{
        text1: '医生姓名',
        text2: '李秀清'
      },
      {
        text1: '订单名称',
        text2: '电话问诊'
      },
      {
        text1: '订单编号',
        text2: '6379287611'
      },
      {
        text1: '患者姓名',
        text2: '啊啊啊'
      }
    ],
    datalist: [], //病例总数组
    sickimg: [], //患处照片
    medimg: [], //药物照片
    sickhisty: [],
    allergy: [],
    hospatil: [], //就诊过的医院
    dochelp: '',
    sicktel: '', //病人电话
    userInfo: {},
    hasuserInfo: false,
    disableJoin: false,
    teamname: false,
  },
  //跳转支付页面
  onGotuserInfo: function(e) {
    var that = this
    var orders = that.data.order
    var uniacid = app.siteInfo.uniacid
    var zid = that.data.zid
    var newmoney = that.data.newmoney
    var z_name = that.data.z_name
    var order = that.data.order
    var oid = that.data.oid
    var bl_id = that.data.bl_id
    //只要咨询就关注医生
    app.util.request({
      'url': 'entry/wxapp/zhuanjia.ifguanzhu',
      data: {
        zid: zid,
        openid: wx.getStorageSync('openid'),
        cerated_type: 0
      },
      success: function(res) {
        console.log(res)
        if (res.data !== 1) {
          //添加关注
          app.util.request({
            'url': 'entry/wxapp/zhuanjia.changelove',
            data: {
              zid: zid,
              openid: wx.getStorageSync('openid'),
              cerated_type: 0
            },
            success: function(res) {
              console.log(res)
            }
          })
        }
      }
    })
    wx.showModal({
      content: '是否支付',
      success: function(res) {
        if (res.confirm) {
          app.util.request({
            'url': that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=Pay&m=hyb_yl',
            header: {
              "Content-Type": "application/xml"
            },
            method: "GET",
            data: {
              openid: wx.getStorageSync('openid'),
              z_tw_money: newmoney
            },
            success: function(res) {
              console.log(res);
              wx.requestPayment({
                'timeStamp': res.data.timeStamp,
                'nonceStr': res.data.nonceStr, //字符串随机数
                'package': res.data.package,
                'signType': res.data.signType,
                'paySign': res.data.paySign,
                success: function(res) {
                  //跳转订单页 改变订单状态
                  //通知医生
                  app.util.request({
                    'url': 'entry/wxapp/Wxmoban.doctemp',
                    data: {
                      openid: wx.getStorageSync('openid'),
                      zid: zid,
                      bingzs: that.data.name,
                      name: that.data.name,
                      oid: oid,
                      bl_id: bl_id,
                      order: order, //改变订单状态,
                      allone_key:that.data.allone_key
                    },
                    success: function(res) {
                      console.log(res)
                    }
                  })
                  console.log(res)
                  var docroom = that.data.docroom
                  //加入数据库在进入房间
                  wx.setStorageSync('docroom', docroom)
                  let userInfo = e.detail.userInfo || {};
                  wx.setStorage({
                    key: 'userInfo',
                    data: userInfo,
                  })
                  var key = that.data.keywords
                  if (key == 'tuwenwenzhen') {
                    var my = wx.getStorageSync("myusername");
                    console.log(my)
                    let sate = 0
                    wx.setStorageSync('sate', sate) //证明是用户发送的信息
                    var nameList = {
                      myName: my,
                      your: docroom
                    };
                    wx.navigateTo({
                      url: '/hyb_yl/czhuanjiasubpages/pages/chatroom/chatroom?username=' + JSON.stringify(nameList) + '&z_name=' + z_name,
                    })
                  }
                  if (key == 'yuanchengwenzhen') {
                    that.onJoin(userInfo);
                  }
                  if (key == 'shoushukuaiyue') {
                    that.onJoin(userInfo);
                  }
                }
              })
            }
          })
        } else {
          //取消支付跳转上一页
          wx.navigateBack({
            detail: 2
          })
        }
      }
    })
  },
  onGotuserInfo2: function(e) {
    var that = this
    var orders = that.data.order
    var q_id = that.data.q_id
    var uniacid = app.siteInfo.uniacid
    var money = that.data.money
    var zid = that.data.zid
    var oid = that.data.oid
    var bl_id = that.data.bl_id
    wx.showModal({
      content: '是否支付',
      success: function(res) {
        if (res.confirm) {
          app.util.request({
            'url': that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=Pay&m=hyb_yl',
            header: {
              "Content-Type": "application/xml"
            },
            method: "GET",
            data: {
              openid: wx.getStorageSync('openid'),
              z_tw_money: money
            },
            success: function(res) {
              console.log(res);
              wx.requestPayment({
                'timeStamp': res.data.timeStamp,
                'nonceStr': res.data.nonceStr, //字符串随机数
                'package': res.data.package,
                'signType': res.data.signType,
                'paySign': res.data.paySign,
                success: function(res) {
                  //改变订单状态
                  console.log(res)
                  //通知医生
                  var qiany = that.data.qiany
                  wx.requestSubscribeMessage({
                    tmplIds: [qiany],
                    success: (res) => {
                      console.log(res)
                      if (res[qiany] === 'accept') {
                        //触发模板消息
                        app.util.request({
                          'url': 'entry/wxapp/office.mbtxing',
                          data: {
                            zid: zid,
                            qiany: qiany,
                            j_id: that.data.j_id
                          },
                          success: function(res) {
                            console.log(res)
                          }
                        })
                        app.util.request({
                          'url': 'entry/wxapp/docuser.updateorder',
                          data: {
                            q_id: q_id
                          },
                          success: function(res) {
                            console.log(res)
                            wx.redirectTo({
                              url: '/hyb_yl/userCommunicate/pages/recordSigning/recordSigning',
                            })
                          }
                        })
                      } else {
                        app.util.request({
                          'url': 'entry/wxapp/docuser.updateorder',
                          data: {
                            q_id: q_id
                          },
                          success: function(res) {
                            console.log(res)
                            wx.redirectTo({
                              url: '/hyb_yl/userCommunicate/pages/recordSigning/recordSigning',
                            })
                          }
                        })
                      }
                    }
                  })
                }
              })
            }
          })
        } else {
          //跳转订单页
          app.util.request({
            'url': 'entry/wxapp/docuser.updatequxiao',
            data: {
              q_id: q_id
            },
            success: function(res) {
              console.log(res)
              wx.reLaunch({
                url: '/hyb_yl/userCommunicate/pages/recordSigning/recordSigning',
              })
            }
          })
        }
      }
    })
  },
  //图文支付
  onGotuserInfo3: function(e) {
    var that = this
    var orders = that.data.order
    var uniacid = app.siteInfo.uniacid
    var zid = that.data.zid
    console.log(zid)
    var newmoney = that.data.newmoney
    console.log(newmoney)
    var z_name = that.data.z_name
    var order = that.data.order
    var bl_id = that.data.bl_id
    //只要咨询就关注医生
    app.util.request({
      'url': 'entry/wxapp/zhuanjia.ifguanzhu',
      data: {
        zid: zid,
        openid: wx.getStorageSync('openid'),
        cerated_type: 0
      },
      success: function(res) {
        console.log(res)
        if (res.data !== 1) {
          //添加关注
          app.util.request({
            'url': 'entry/wxapp/zhuanjia.changelove',
            data: {
              zid: zid,
              openid: wx.getStorageSync('openid'),
              cerated_type: 0
            },
            success: function(res) {
              console.log(res)
            }
          })
        }
      }
    })
    wx.showModal({
      content: '是否支付',
      success: function(res) {
        if (res.confirm) {
          var doctemp = that.data.doctemp
          app.util.request({
            'url': that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=Pay&m=hyb_yl',
            header: {
              "Content-Type": "application/xml"
            },
            method: "GET",
            data: {
              openid: wx.getStorageSync('openid'),
              z_tw_money: newmoney
            },
            success: function(res) {
              console.log(res);
              wx.requestPayment({
                'timeStamp': res.data.timeStamp,
                'nonceStr': res.data.nonceStr, //字符串随机数
                'package': res.data.package,
                'signType': res.data.signType,
                'paySign': res.data.paySign,
                success: function(res) {
                  console.log(res)
                  //跳转订单页 改变订单状态
                  //通知医生
                  wx.requestSubscribeMessage({
                    tmplIds: [doctemp],
                    success: (res) => {
                      if (res[doctemp] === 'accept') {
                        //触发模板消息
                        app.util.request({
                          'url': 'entry/wxapp/tuwen.mbtxing',
                          data: {
                            zid: zid,
                            doctemp: doctemp,
                            j_id: that.data.j_id
                          },
                          success: function(res) {
                            console.log(res)
                          }
                        })
                        var docroom = that.data.docroom
                        wx.setStorageSync('docroom', docroom)
                        let userInfo = e.detail.userInfo || {};
                        wx.setStorage({
                          key: 'userInfo',
                          data: userInfo,
                        })
                        var key = that.data.keywords
                        if (key == 'tuwenwenzhen') {
                          var my = wx.getStorageSync("myusername");
                          console.log(my)
                          let sate = 0
                          wx.setStorageSync('sate', sate) //证明是用户发送的信息
                          var nameList = {
                            myName: my,
                            your: docroom
                          };
                          wx.navigateTo({
                            url: '/hyb_yl/czhuanjiasubpages/pages/chatroom/chatroom?username=' + JSON.stringify(nameList) + '&z_name=' + z_name,
                          })
                        }
                      } else {
                        var docroom = that.data.docroom
                        wx.setStorageSync('docroom', docroom)
                        let userInfo = e.detail.userInfo || {};
                        wx.setStorage({
                          key: 'userInfo',
                          data: userInfo,
                        })
                        var key = that.data.keywords
                        if (key == 'tuwenwenzhen') {
                          var my = wx.getStorageSync("myusername");
                          console.log(my)
                          let sate = 0
                          wx.setStorageSync('sate', sate) //证明是用户发送的信息
                          var nameList = {
                            myName: my,
                            your: docroom
                          };
                          wx.navigateTo({
                            url: '/hyb_yl/czhuanjiasubpages/pages/chatroom/chatroom?username=' + JSON.stringify(nameList) + '&z_name=' + z_name,
                          })
                        }
                      }
                    }
                  })
                },
                fail:function(res){
                  console.log(res)
                }
              })
            }
          })
        } else {
          //取消支付跳转上一页
          wx.navigateBack({
            detail: 1
          })
        }
      }
    })
  },
  checkJoinLock: function() {
    return !(this.lock || false);
  },

  lockJoin: function() {
    this.lock = true;
  },

  unlockJoin: function() {
    this.lock = false;
  },
  onJoin: function(userInfo) {
    userInfo = userInfo || {};
    var order = this.data.order
    let value = this.channel || order;
    let uid = this.uid;
    if (!value) {
      wx.showToast({
        title: '请提供一个有效的房间名',
        icon: 'none',
        duration: 2000
      })
    } else {
      if (this.checkJoinLock()) {
        this.lockJoin();
        let role = "audience";
        role = "broadcaster";
        wx.navigateTo({
          url: `../meeting/meeting?channel=${value}&uid=${uid}&role=${role}`
        });
      }
    }
  },

  onInputChannel: function(e) {
    let value = e.detail.value;
    this.channel = value;
  },
  //下一页
  nextpage: function() {
    wx.navigateTo({
      url: '/hyb_yl/czhuanjiasubpages/pages/telserev/index',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    console.log(options)
    var that = this
    that.channel = "";
    that.uid = Utils.getUid();
    that.lock = false;
    let userInfo = wx.getStorageSync("userInfo");
    var zid = options.zid
    var j_id = options.j_id
    var allone_key = options.allone_key
    var name = options.name
    var order = options.order
    var money = options.money
    var keywords = options.keywords
    var docroom = options.docroom
    var myroom = wx.getStorageSync('myusername')
    if (userInfo) {
      that.setData({
        hasuserInfo: true,
        userInfo: userInfo
      });
    }
    if (options.teamname) {
      app.util.request({
        'url': 'entry/wxapp/office.bmtempetid',
        success: function(res) {
          console.log(res)
          that.setData({
            qiany: res.data.qiany
          })
        }
      })
      wx.setNavigationBarTitle({
        title: '收银台'
      })
      that.setData({
        teamname: options.teamname,
        money: options.money,
        order: options.order,
        q_id: options.q_id
      });
    }
    if (allone_key == 1) {
      app.util.request({
        'url': 'entry/wxapp/office.bmtempetid',
        success: function(res) {
          console.log(res)
          that.setData({
            doctemp: res.data.doctemp
          })
        }
      })
      var oid = options.oid
      var bl_id = options.bl_id
      wx.setNavigationBarTitle({
        title: '收银台'
      })
      that.setData({
        oid: oid,
        bl_id: bl_id
      })
    }
    if (allone_key == 2) {
      var bl_id = options.bl_id
      var j_id = options.j_id
      wx.setNavigationBarTitle({
        title: '收银台'
      })
      app.util.request({
        'url': 'entry/wxapp/office.bmtempetid',
        success: function(res) {
          console.log(res)
          that.setData({
            doctemp: res.data.doctemp
          })
        }
      })
      that.setData({
        bl_id: bl_id,
        j_id: j_id
      })
    }
    if (options.datakey == 2) {
      var datakey = options.datakey
      var oid = options.oid
      that.setData({
        datakey: datakey
      })
      //后端返回医生查看病例
      app.util.request({
        'url': 'entry/wxapp/hzbingli.seeuserbl',
        data: {
          oid: oid
        },
        success: function(res) {
          console.log(res)
          var msglist = res.data.msglist
          that.setData({
            datalist: msglist
          })
        }
      })
      that.setData({
        oid: oid
      })
    }
    app.util.request({
      'url': 'entry/wxapp/hzbingli.url',
      success: function(res) {
        console.log(res)
        that.setData({
          url: res.data,
        })
      }
    })
    that.setData({
      sicktel: options.phone,
      allone_key: allone_key,
      name: name,
      money: money,
      order: order,
      zid: zid,
      keywords: keywords,
      docroom: docroom
    })
    var datalist = that.data.datalist
    var that = this
    app.util.request({
      'url': 'entry/wxapp/zhuanjia.docinfo',
      data: {
        zid: zid,
        key: keywords,
      },
      success: function(res) {
        console.log(res)
        that.setData({
          newmoney: res.data.data.newmoney
        })
      }
    })
    app.util.request({
      'url': 'entry/wxapp/user.detail',
      data: {
        j_id: j_id
      },
      success: function(res) {
        console.log(res)
        that.setData({
          usernames: res.data.names
        })
      }
    })
    app.util.request({
      'url': 'entry/wxapp/zhuanjia.docinfo',
      data: {
        zid: zid
      },
      success: function(res) {
        console.log(res)
        that.setData({
          z_name: res.data.data.z_name
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    wx.navigateBack({
      detail: 3
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },
  //使用该病例
  usecase() {
    //接诊

  },
})